<?php
class response implements gatekeeper
{
	function __construct()
	{
		Global $lib, $mySql, $auth;
		$this->lib = $lib;
		$this->mySql = $mySql;
		$this->auth = $auth;
		if(!EMPTY($_GET["pageaction"]))
		{
			$this->PageAction = $_GET["pageaction"];
		}
	}
	function runClass()
	{
		#############################
		#								#
		#		Auth Logic	 				#
		#								#
		#############################
		// Find Login Status
		$this->auth->runClass();
		if($this->auth->getLoginStatus())
		{
			$this->LoginStatus = true;
			$this->Login = $this->auth->getLoginInfo();
		}
		else
		{
			$this->LoginStatus = false;
		}
		
		#############################
		#								#
		#		Back-End Logic 				#
		#								#
		#############################
		
		if("read" == $this->PageAction)
		{
			jsresponse::readMessages($this->Login);
		}
		elseif("author" == $this->PageAction)
		{
			$this->setField("id"); // The Author ID
			$this->setField("name"); // The Author Name
			if(!EMPTY($this->id) || !EMPTY($this->name))
			{
				// Read out messages
			}
			else
			{
				die("Failed. Correct Parameters not given");
			}
			exit();
		}
		elseif("message" == $this->PageAction)
		{
			$this->setField("id"); // The Individual Message ID
			if(!EMPTY($this->id))
			{
				// Display Individual Message
			}
			else
			{
				die("Failed. Correct Parameters not given");
			}
		}
		elseif("write" == $this->PageAction)
		{
			if($this->LoginStatus == true)
			{
				$this->setField("message");
				if(!EMPTY($this->message))
				{
					// Write the Message
				}
				else
				{
					die("Failed. Correct Parameters not given");
				}
			}
			else
			{
				echo "Access Denied. Not Logged In";
				exit();
			}
		}
		elseif("vote" == $this->PageAction)
		{
			if($this->LoginStatus == true)
			{
				$this->setField("action");
				$this->setField("id");
				if(!EMPTY($this->action) && !EMPTY($this->id))
				{
					jsresponse::voteMessage($this->Login["login"]['id'], $this->action, $this->id);
				}
				else
				{
					die("Failed. Correct Parameters not given");
				}				
			}
			else
			{
				echo "Access Denied. Not Logged In";
				exit();
			}
		}
		elseif("filter" == $this->PageAction)
		{
			if($this->LoginStatus == true)
			{
				$this->setField("feed");
				if(!EMPTY($this->feed))
				{
					jsresponse::filterFeed($this->feed);
					echo "DONE";
				}
				else
				{
					die("Failed. Correct Parameters not given");
				}	
			}
			else
			{
				echo "Access Denied. Not Logged In";
				exit();
			}
		}
		else
		{
			echo "<i>Internal Error</i>";
		}

	}
	private function setField($field, $value = FALSE) 
	{ 
		// Manuall Set Value 
		if (FALSE != @$value) 
		{ 
			$this->$field = $value; 
		} 
		// Form Data First 
		elseif (FALSE != @$_POST["form"][$field]) 
		{ 
			$this->$field = $_POST["form"][$field]; 
		} 
		// General Post data next 
		elseif (FALSE != @$_POST[$field]) 
		{ 
			$this->$field = $_POST[$field]; 
		} 
		// Get Data last 
		elseif (FALSE != @$_GET[$field]) 
		{ 
			$this->$field = $_GET[$field]; 
		} 
		// But it can't be an array 
		if (FALSE != @is_array($this->$field)) 
		{ 
			unset($this->$field); 
		} 
	}

}
